﻿using HalconDotNet;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HalconExtension.Models
{
    /// <summary>
    /// 矩形封装
    /// </summary>
    public class HRectangleObj : HObjBase
    {
        public HTuple Row1 { get; private set; }
        public HTuple Row2 { get; private set; }
        public HTuple Column1 { get; private set; }
        public HTuple Column2 { get; private set; }

        /// <summary>
        /// 刷新回调
        /// </summary>
        public Action<HRectangleObj>? RefleshCallBack { get; set; }

        /// <summary>
        /// 初始化构造函数
        /// </summary>
        /// <param name="windowControlWPF"></param>
        /// <param name="row1"></param>
        /// <param name="column1"></param>
        /// <param name="row2"></param>
        /// <param name="column2"></param>
        public HRectangleObj(HSmartWindowControlWPF windowControlWPF, HImage hImage, HTuple row1, HTuple column1, HTuple row2, HTuple column2)
        {
            HType = "rect";
            WindowControlWPF = windowControlWPF;
            Image = hImage;
            //WindowControlWPF = windowControlWPF;
            Row1 = row1;
            Column1 = column1;
            Row2 = row2;
            Column2 = column2;
            DrawingObject = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE1, Row1, Column1, Row2, Column2);

            //添加更新事件
            DrawingObject.OnDrag(Reflash);
            DrawingObject.OnResize(Reflash);
        }

        /// <summary>
        /// 默认构造函数
        /// </summary>
        /// <param name="windowControl"></param>
        public HRectangleObj(HSmartWindowControlWPF windowControl, HImage hImage) : this(windowControl, hImage, 100, 200, 300, 400)
        {


        }

        public HRectangleObj()
        {

        }



        /// <summary>
        /// 序列化输出
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {

            return JsonConvert.SerializeObject(new
            {
                type = HType.ToString(),
                row1 = TwoDecimalPlaces(Row1),
                column1 = TwoDecimalPlaces(Column1),
                row2 = TwoDecimalPlaces(Row2),
                column2 = TwoDecimalPlaces(Column2)
            });
        }

        public override void Reflash(HDrawingObject drawid, HWindow window, string type)
        {
            Row1 = drawid.GetDrawingObjectParams("row1");
            Row2 = drawid.GetDrawingObjectParams("row2");
            Column1 = drawid.GetDrawingObjectParams("column1");
            Column2 = drawid.GetDrawingObjectParams("column2");

            //将本身回调
            RefleshCallBack(this);
        }
        /// <summary>
        /// 截取图片
        /// </summary>
        public override HObject Reduce()
        {
            HObject res = new HObject();
            HObject part = new HObject();
            HOperatorSet.GenRectangle1(out ROI, Row1, Column1, Row2, Column2);
            HOperatorSet.ReduceDomain(Image, ROI, out part);
            HOperatorSet.CropDomain(part, out res);
            return res;
        }

        /// <summary>
        /// 绘制图片，返回回调函数
        /// </summary>
        /// <param name="callback"></param>
        /// <returns></returns>
        public override Task Draw(Action callback)
        {
            //防止出现多个鼠标绘制任务。强制前一个任务放开
            HalconAPI.CancelDraw();
            HTuple row1 = new HTuple();
            HTuple row2 = new HTuple();
            HTuple column1 = new HTuple();
            HTuple column2 = new HTuple();

            //double row1,row2,column1,column2;
            HOperatorSet.DrawRectangle1(WindowControlWPF.HalconWindow, out row1, out column1, out row2, out column2);
            //WindowControlWPF.HalconWindow.DrawRectangle1(out row1, out column1, out row2, out column2);
            Row1 = row1;
            Row2 = row2;
            Column1 = column1;
            Column2 = column2;

            //获取接触的ROI区域
            Reduce();

            callback();
            return Task.CompletedTask;
        }
    }
}
